package org.util;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.data.Event;
import org.data.EventORace;
import org.data.EventRunRace;
import org.data.ORace;
import org.data.RunRace;
import org.data.SportsmanProfile;
import org.h2.jaqu.*;

/**
 *
 * This class create table structure in database and fill it with test data.
 * Make sure that all tables are deleted from database before run it.
 * 
 * @author George.Mavchun
 */
public class DBInit {

    static Db db;

    public static void main(String[] args) throws Exception {
        db = Db.open("jdbc:h2:file:~/data/OSDDB.db", "sa", "");

        // Add SportsmanProfile and Event records
        IStep();

        // Add RunRace and EventRunRace records
        IIStep();

        // Add ORace and EventORace records
        IIIStep();

        db.close();
    }

    /**
     * This method adds to database Event and SportsmanProfile records
     * 100 sportsmen with 100 events for each.
     *
     */
    private static void IStep() {
        List<SportsmanProfile> profiles = new ArrayList<SportsmanProfile>();
        List<Event> events = new ArrayList<Event>();
        String password = Util.getMD5Hash("test");
        for (int i = 1; i <= 1000; i++) {
            profiles.add(new SportsmanProfile(i, "test" + i, password, "name" + i, "surname" + i, null, "club" + i, null, null, null, "city" + i));
            for (int j = 1; j <= 100; j++) {
                events.add(new Event((i - 1) * 100000 + j, i, "event name " + i + "_" + j, "place" + j, new Timestamp(i * 1000)));
            }
        }
        db.insertAll(profiles);
        db.insertAll(events);
    }

    private static void IIStep() {
        List<RunRace> runRaces = new ArrayList<RunRace>();
        List<EventRunRace> eventRunRaces = new ArrayList<EventRunRace>();
        for (int i = 1; i <= 10; i++) {
            runRaces.add(new RunRace(i, 1000, Time.valueOf("00:45:00"), 10, 10, 10, 10, "description" + i));
            eventRunRaces.add(new EventRunRace(1, i));
        }
        db.insertAll(runRaces);
        db.insertAll(eventRunRaces);
    }

    private static void IIIStep() {
        List<ORace> oRaces = new ArrayList<ORace>();
        List<EventORace> eventORaces = new ArrayList<EventORace>();
        for (int i = 1; i <= 10; i++) {
            oRaces.add(new ORace(i, 1000, Time.valueOf("00:45:00"), 10, 10, 10, 10, 10, "link", "description" + i));
            eventORaces.add(new EventORace(1, i));
        }
        db.insertAll(oRaces);
        db.insertAll(eventORaces);
    }
}
